package ch3stacks;

import java.util.Stack;

@SuppressWarnings("serial")
class MinStack extends Stack<Integer> {
	Stack<Integer> mins = new Stack<>();
	
	@Override
	public Integer push(Integer e) {
		Integer push = super.push(e);
		if (e < getMin()) {
			mins.push(e);
		}
		return push;
	}
	
	@Override
	public Integer pop() {
		Integer pop = super.pop();
		if (pop.intValue() == getMin()) {
			mins.pop();
		}
		return pop;
	}

	public int getMin() {
		if (mins.empty()) {
			return Integer.MAX_VALUE;
		}
		else {
			Integer peek = mins.peek();
			return peek;
		}
	}
}

public class Ex2 {
	public static void main(String ...strings) {
		int [] input = new int [] {3, 2, 1, 2, 0};
		MinStack minStack = new MinStack();
		for (int i : input) {
			minStack.push(i);
		}
		
		System.out.println(minStack);
		System.out.println("min="+minStack.getMin());

		minStack.pop();
		System.out.println(minStack);
		System.out.println("min="+minStack.getMin());

		minStack.push(2);
		System.out.println(minStack);
		System.out.println("min="+minStack.getMin());

		minStack.push(0);
		System.out.println(minStack);
		System.out.println("min="+minStack.getMin());
	}
}
